package cn.iocoder.yudao.module.nginxui.controller.admin.config;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.config.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.config.NginxConfigDO;
import cn.iocoder.yudao.module.nginxui.service.config.NginxConfigService;

@Tag(name = "管理后台 - Nginx主配置")
@RestController
@RequestMapping("/nginxui/config")
@Validated
public class NginxConfigController {

    @Resource
    private NginxConfigService configService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx主配置")
    @PreAuthorize("@ss.hasPermission('nginxui:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody ConfigSaveReqVO createReqVO) {
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx主配置")
    @PreAuthorize("@ss.hasPermission('nginxui:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigSaveReqVO updateReqVO) {
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx主配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx主配置")
                @PreAuthorize("@ss.hasPermission('nginxui:config:delete')")
    public CommonResult<Boolean> deleteConfigList(@RequestParam("ids") List<Long> ids) {
        configService.deleteConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx主配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:config:query')")
    public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) {
        NginxConfigDO config = configService.getConfig(id);
        return success(BeanUtils.toBean(config, ConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx主配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:config:query')")
    public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO pageReqVO) {
        PageResult<NginxConfigDO> pageResult = configService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx主配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigExcel(@Valid ConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NginxConfigDO> list = configService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx主配置.xls", "数据", ConfigRespVO.class,
                        BeanUtils.toBean(list, ConfigRespVO.class));
    }

}